/******************************************************************************
 * * sms_common.c - implementation of sms common functions
 *
 * *(C) Copyright 2019 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/
#include "sms_include.h"

extern TASK_HANDLE *gui_task_id;

#if USE_LV_WATCH_MODEM_ADAPTOR
void sms_send_result_ind(watch_app_adp_sms_send_result_type_t result)
{
    if(0 == Hal_Pm_Get_State()) return;

    mmi_msg_send_sms_cnf_t * msg;

    msg = (mmi_msg_send_sms_cnf_t *)lv_mem_alloc(sizeof(mmi_msg_send_sms_cnf_t));

    msg->header.MsgId = MMI_MODEM_SEND_SMS_CNF;
    msg->psendsmscnf = (MMI_Modem_Send_Sms_Cnf_t *)lv_mem_alloc(sizeof(MMI_Modem_Send_Sms_Cnf_t));

    Hal_Mem_Set(msg->psendsmscnf,0,sizeof(MMI_Modem_Send_Sms_Cnf_t));

    msg->psendsmscnf->SimId = g_MsgGlobalCtrl.CurrSimId;
    msg->psendsmscnf->MsgIndex = 0;

    if(APP_SMS_SEND_SUCCESS == result)
    {
        msg->psendsmscnf->Result = MMI_MODEM_SMS_SUCCESS;
    }
    else
    {
        msg->psendsmscnf->Result = MMI_MODEM_SMS_FAIL;
    }

    Hal_Send_Message((TASK_ID)gui_task_id, (void *)msg);
}
#else
void sms_send_result_ind(watch_app_adp_sms_send_result_type_t result)
{

}
#endif
/**
 * initiate sms module
 * param (in) VOID
 * return  VOID
 */
VOID Sms_Com_Send_Sms_To_One_Req(VOID)
{
#if 0
    UINT32                   i                       = 0;
#endif
    char*                    number                  = NULL;
    char*                    smspdu                  = NULL;
    UINT32                    len                     = 0;
#if 0
    MMI_Modem_Send_Sms_Req_t *pSendSms               = NULL;
#endif
    Msg_Content_Info_t       *pSentboxMsgContentInfo = NULL;

#if 0
    // start send message
    pSendSms = (MMI_Modem_Send_Sms_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modem_Send_Sms_Req_t));
    Hal_Mem_Set(pSendSms, 0, sizeof(MMI_Modem_Send_Sms_Req_t));
    // get utc time
    // encode sms
    if (MMI_MODEM_SIM_1 == g_MsgGlobalCtrl.CurrSimId)
    {
        i = strlen(g_MsgGlobalCtrl.Smsc[0].pSmsc);
    }
    else
    {
        i = strlen(g_MsgGlobalCtrl.Smsc[1].pSmsc);
    }
    pSendSms->pSmsc = Hal_Mem_Alloc(i + 1);
    Hal_Mem_Set(pSendSms->pSmsc, 0, i + 1);
    if (MMI_MODEM_SIM_1 == g_MsgGlobalCtrl.CurrSimId)
    {
        strcpy(pSendSms->pSmsc, g_MsgGlobalCtrl.Smsc[0].pSmsc);
    }
    else
    {
        strcpy(pSendSms->pSmsc, g_MsgGlobalCtrl.Smsc[1].pSmsc);
    }
    pSendSms->SimId = g_MsgGlobalCtrl.CurrSimId;
    // pSendSms->pSmsPdu = ;
    pSendSms->WriteToSimFlg = MMI_MODEM_WRITE_TO_SIM;
#endif
    if(NULL != g_MsgGlobalCtrl.pContact) {
        len = strlen(g_MsgGlobalCtrl.pContact);
    } else {
        len = 0;
    }
    number = Hal_Mem_Alloc(len + 1);
    Hal_Mem_Set(number,0,len + 1);
    Hal_Mem_Copy(number,g_MsgGlobalCtrl.pContact,len);

if(NULL != g_MsgGlobalCtrl.pSmsText) {
        len = strlen(g_MsgGlobalCtrl.pSmsText);
    } else {
        len = 0;
    }
    smspdu = Hal_Mem_Alloc(len + 1);
    Hal_Mem_Set(smspdu,0,len + 1);
    Hal_Mem_Copy(smspdu,g_MsgGlobalCtrl.pSmsText,len);

#if MMI_MODEM_SMS_STORE_IN_FILE != 0
    //to update sms file
    MMI_Modem_Sms_File_Send_Sms_Req(number, smspdu, (MMI_MODEM_SIM_ID)g_MsgGlobalCtrl.CurrSimId);
#endif

#if USE_LV_WATCH_MODEM_ADAPTOR
    app_adaptor_sms_send_req(number, smspdu, strlen(smspdu), NULL, sms_send_result_ind,(uint8_t)g_MsgGlobalCtrl.CurrSimId);
#else
    Hal_Mem_Free(number);
    Hal_Mem_Free(smspdu);
#endif

#if 0
    MMI_Modem_Send_Sms_Req(pSendSms);
#endif
    if (NULL == g_MsgGlobalCtrl.pSentboxMsgContentInfo)
    {
        g_MsgGlobalCtrl.pSentboxMsgContentInfo = (lv_ll_t *)Hal_Mem_Alloc(sizeof(lv_ll_t));
        Hal_Mem_Set(g_MsgGlobalCtrl.pSentboxMsgContentInfo, 0, sizeof(lv_ll_t));
        _lv_ll_init(g_MsgGlobalCtrl.pSentboxMsgContentInfo, sizeof(Msg_Content_Info_t));
        pSentboxMsgContentInfo = _lv_ll_ins_head(g_MsgGlobalCtrl.pSentboxMsgContentInfo);
        Hal_Mem_Set(pSentboxMsgContentInfo, 0, sizeof(Msg_Content_Info_t));
    }
    else
    {
        pSentboxMsgContentInfo = _lv_ll_ins_head(g_MsgGlobalCtrl.pSentboxMsgContentInfo);
        Hal_Mem_Set(pSentboxMsgContentInfo, 0, sizeof(Msg_Content_Info_t));
    }
    pSentboxMsgContentInfo->pContact = Hal_Mem_Alloc(1);
    pSentboxMsgContentInfo->SimId    = g_MsgGlobalCtrl.CurrSimId;
} /* Sms_Com_Send_Sms_To_One_Req */

/**
 * initiate sms module
 * param (in) VOID
 * return  VOID
 */
VOID Sms_Com_Send_Sms_To_List_Req(VOID)
{
#if 0
    UINT32                   i                       = 0;
#endif
    char*                    number                  = NULL;
    char*                    smspdu                  = NULL;
#if 0
    MMI_Modem_Send_Sms_Req_t *pSendSms               = NULL;
#endif
    Msg_Content_Info_t       *pSentboxMsgContentInfo = NULL;

#if 0
    // start send messages
    pSendSms = (MMI_Modem_Send_Sms_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modem_Send_Sms_Req_t));
    Hal_Mem_Set(pSendSms, 0, sizeof(MMI_Modem_Send_Sms_Req_t));
    // get utc time
    // encode sms
    if (MMI_MODEM_SIM_1 == g_MsgGlobalCtrl.CurrSimId)
    {
        i = strlen(g_MsgGlobalCtrl.Smsc[0].pSmsc);
    }
    else
    {
        i = strlen(g_MsgGlobalCtrl.Smsc[1].pSmsc);
    }

    pSendSms->pSmsc = Hal_Mem_Alloc(i + 1);
    Hal_Mem_Set(pSendSms->pSmsc, 0, i + 1);
    if (MMI_MODEM_SIM_1 == g_MsgGlobalCtrl.CurrSimId)
    {
        strcpy(pSendSms->pSmsc, g_MsgGlobalCtrl.Smsc[0].pSmsc);
    }
    else
    {
        strcpy(pSendSms->pSmsc, g_MsgGlobalCtrl.Smsc[1].pSmsc);
    }
    pSendSms->SimId = g_MsgGlobalCtrl.CurrSimId;
    // pSendSms->pSmsPdu = ;
    pSendSms->WriteToSimFlg = MMI_MODEM_WRITE_TO_SIM;
    MMI_Modem_Send_Sms_Req(pSendSms);
#endif
    number = Hal_Mem_Alloc(strlen(g_MsgGlobalCtrl.pCurrentSend->pNum) + 1);
    Hal_Mem_Set(number,0,strlen(g_MsgGlobalCtrl.pCurrentSend->pNum) + 1);
    Hal_Mem_Copy(number,g_MsgGlobalCtrl.pCurrentSend->pNum,strlen(g_MsgGlobalCtrl.pCurrentSend->pNum));

    smspdu = Hal_Mem_Alloc(strlen(g_MsgGlobalCtrl.pSmsText) + 1);
    Hal_Mem_Set(smspdu,0,strlen(g_MsgGlobalCtrl.pSmsText) + 1);
    Hal_Mem_Copy(smspdu,g_MsgGlobalCtrl.pSmsText,strlen(g_MsgGlobalCtrl.pSmsText));

#if USE_LV_WATCH_MODEM_ADAPTOR
    app_adaptor_sms_send_req(number, smspdu, strlen(smspdu), NULL, sms_send_result_ind,(uint8_t)g_MsgGlobalCtrl.CurrSimId);
#else
    Hal_Mem_Free(number);
    Hal_Mem_Free(smspdu);
#endif
    if (NULL == g_MsgGlobalCtrl.pSentboxMsgContentInfo)
    {
        g_MsgGlobalCtrl.pSentboxMsgContentInfo = (lv_ll_t *)Hal_Mem_Alloc(sizeof(lv_ll_t));
        Hal_Mem_Set(g_MsgGlobalCtrl.pSentboxMsgContentInfo, 0, sizeof(lv_ll_t));
        _lv_ll_init(g_MsgGlobalCtrl.pSentboxMsgContentInfo, sizeof(Msg_Content_Info_t));
        pSentboxMsgContentInfo = _lv_ll_ins_head(g_MsgGlobalCtrl.pSentboxMsgContentInfo);
        Hal_Mem_Set(pSentboxMsgContentInfo, 0, sizeof(Msg_Content_Info_t));
    }
    else
    {
        pSentboxMsgContentInfo = _lv_ll_ins_head(g_MsgGlobalCtrl.pSentboxMsgContentInfo);
        Hal_Mem_Set(pSentboxMsgContentInfo, 0, sizeof(Msg_Content_Info_t));
    }
    pSentboxMsgContentInfo->pContact = Hal_Mem_Alloc(1);
    pSentboxMsgContentInfo->SimId    = g_MsgGlobalCtrl.CurrSimId;
} /* Sms_Com_Send_Sms_To_List_Req */

/**
 * sub-module entry
 * param (in) VOID
 * return  VOID
 */
VOID Sms_Timer_Create_And_Start(CB_FUNC callback, UINT32 id)
{
    static UINT32 ActId;
    ActId = id;
    g_MsgGlobalCtrl.Timerid = Hal_Timer_Start(MSG_TIMER_LENGTH, callback, (VOID *)&ActId, FALSE);
}

/**
 * sub-module entry
 * param (in) VOID
 * return  VOID
 */
VOID Sms_Timer_Stop_And_Destroy(VOID* id)
{
#if 0
    UINT32* Id = (UINT32 *)id;
    if (MSG_INVAILD_TIMER_ID != *Id)
    {
        Hal_Timer_Stop(g_MsgGlobalCtrl.Timerid);
    }
#endif
}

/**
 * sub-module entry
 * param (in) INT8 *Report
 * return  VOID
 */
VOID Sms_Message_Sent_Rep(INT8 *Report)
{
    CB_FUNC          callback = Sms_Message_Sent_Rep_Timeout;
    Nav_Func_List_t  FuncList;
    UI_Status_Desc_t *pSendResult = NULL;

    pSendResult = (UI_Status_Desc_t *)Hal_Mem_Alloc(sizeof(UI_Status_Desc_t));
    Hal_Mem_Set(pSendResult, 0, sizeof(UI_Status_Desc_t));

    pSendResult->ContLabel.LabelAlign = LV_ALIGN_IN_TOP_LEFT;
    pSendResult->ContLabel.TxtId      = LV_LANG_TXT_ID_NONE;
    pSendResult->ContLabel.TxtAlign   = LV_LABEL_ALIGN_LEFT;
    pSendResult->ContLabelAlign       = LV_ALIGN_IN_TOP_LEFT;

    pSendResult->ContImg.Img = SYMBOL_CHECKED;

    pSendResult->ContImg.ImgAlign = LV_ALIGN_IN_TOP_RIGHT;
    pSendResult->ContImgAlign     = LV_ALIGN_IN_TOP_RIGHT;

    FuncList.OnShow         = (Void_P_Func_t)UI_Status_Create;
    FuncList.OnHide         = NULL;
    FuncList.OnCreate       = Sms_Common_Create;
    FuncList.OnDestroy      = Sms_Common_Destory; // temp,delete all global
    FuncList.OnSaveState    = Sms_Common_Save;    // temp
    FuncList.OnRestoreState = Sms_Common_Restore; // temp

    Nav_Forward(ACT_ID_ANY, ACT_ID_MESSAGES_MESSAGE_SENT, &FuncList, (void *)pSendResult);
    lv_label_set_text(pSendResult->ContLabel.Label, Report);
    // 2s timer start
    Sms_Timer_Create_And_Start(callback, ACT_ID_MESSAGES_MESSAGE_SENT);
}

/**
 * sub-module entry
 * param (in) VOID
 * return  VOID
 */
VOID Sms_Message_Sent_Rep_Timeout(VOID* id)
{
    UINT32* Id = (UINT32 *)id;
    Nav_Back(*Id);
}

/**
 * sub-module entry
 * param (in) VOID
 * return  VOID
 */
VOID Sms_Sim_Storeage_Full(MMI_MODEM_SIM_ID SimId)
{
    CB_FUNC          callback = Sms_Sim_Storeage_Full_Timeout;
    Nav_Func_List_t  FuncList;
    UI_Status_Desc_t *pStoreageStsInd = NULL;
    INT8             str[30];
    INT8             *pTemp = NULL;

    pStoreageStsInd = (UI_Status_Desc_t *)Hal_Mem_Alloc(sizeof(UI_Status_Desc_t));
    Hal_Mem_Set(pStoreageStsInd, 0, sizeof(UI_Status_Desc_t));

    pStoreageStsInd->ContLabel.LabelAlign = LV_ALIGN_IN_TOP_LEFT;
    pStoreageStsInd->ContLabel.TxtId      = PHONE_TEXT_ID_SMS_STOREAGE_FULL;
    pStoreageStsInd->ContLabel.TxtAlign   = LV_LABEL_ALIGN_LEFT;
    pStoreageStsInd->ContLabelAlign       = LV_ALIGN_IN_TOP_LEFT;

    pStoreageStsInd->ContImg.Img = SYMBOL_CHECKED;

    pStoreageStsInd->ContImg.ImgAlign = LV_ALIGN_IN_TOP_RIGHT;
    pStoreageStsInd->ContImgAlign     = LV_ALIGN_IN_TOP_RIGHT;

    FuncList.OnShow         = (Void_P_Func_t)UI_Status_Create;
    FuncList.OnHide         = NULL;
    FuncList.OnCreate       = Sms_Common_Create;
    FuncList.OnDestroy      = Sms_Common_Destory;
    FuncList.OnSaveState    = Sms_Common_Save;
    FuncList.OnRestoreState = Sms_Common_Restore;

    Nav_Forward(ACT_ID_ANY, ACT_ID_MESSAGES_SIM_STOREAGE_FULL, &FuncList, (void *)pStoreageStsInd);

    Hal_Mem_Set(str, 0, 30);
    if (MMI_MODEM_SIM_1 == SimId)
    {
        pTemp = UI_Lang_Get_Text_ById(PHONE_TEXT_ID_SIM1);
    }
    else
    {
        pTemp = UI_Lang_Get_Text_ById(PHONE_TEXT_ID_SIM2);
    }
    strcpy(str, pTemp);
    pTemp = UI_Lang_Get_Text_ById(PHONE_TEXT_ID_SMS_STOREAGE_FULL);
    strcat(str, pTemp);
    lv_label_set_text(pStoreageStsInd->ContLabel.Label, str);
    // 2s timer start
    Sms_Timer_Create_And_Start(callback, ACT_ID_MESSAGES_MESSAGE_SENT);
} /* Sms_Sim_Storeage_Full */

/**
 * sub-module entry
 * param (in) VOID
 * return  VOID
 */
VOID Sms_Sim_Storeage_Full_Timeout(VOID* id)
{
    UINT32* Id = (UINT32 *)id;
    Nav_Back(*Id);
}

/**
 * Common Process UI Create event
 * param (in) pDesc:  VOID *
 * return  void
 */
VOID Sms_Common_Create(VOID *pDesc)
{
    g_MsgGlobalCtrl.pChart = pDesc;
}

/**
 * Common Process UI destroy event
 * param (in) pDesc:  VOID *
 * return  void
 */
VOID Sms_Common_Destory(VOID *pDesc)
{
    Hal_Mem_Free(pDesc);
    pDesc                  = NULL;
    g_MsgGlobalCtrl.pChart = NULL;
}

/**
 * Common Process UI save event
 * param (in) pDesc:  VOID *
 * return  void
 */
VOID Sms_Common_Save(VOID *pDesc)
{
    g_MsgGlobalCtrl.pChart = NULL;
}

/**
 * Common Process UI restore event
 * param (in) pDesc:  VOID *
 * return  void
 */
VOID Sms_Common_Restore(VOID *pDesc)
{
    g_MsgGlobalCtrl.pChart = pDesc;
}

/**
 * Common Process UI destroy event
 * param (in) pDesc:  VOID *
 * return  void
 */
VOID Sms_List_Only_Common_Destory(VOID *pDesc)
{
    UI_Normal_List_Only_Desc_t *pInput = pDesc;

    Hal_Mem_Free(pInput->List.ButtonList);
    pInput->List.ButtonList = NULL;
    g_MsgGlobalCtrl.pChart  = NULL;
    Hal_Mem_Free(pDesc);
    pDesc = NULL;
}

/**
 * Common Process UI destroy event
 * param (in) lv_ll_t *pMsgContentInfo
 * return  void
 */
VOID Sms_Common_Free_Msg_Content_Info(lv_ll_t **pMsgContentInfo)
{
    Msg_Content_Info_t *pNode = NULL;

    if (NULL != *pMsgContentInfo)
    {
        pNode = _lv_ll_get_head(*pMsgContentInfo);
        while (NULL != pNode)
        {
            Hal_Mem_Free(pNode->pContact);
            pNode->pContact = NULL;
            pNode           = _lv_ll_get_next(*pMsgContentInfo, pNode);
        }
        _lv_ll_clear(*pMsgContentInfo);
        Hal_Mem_Free(*pMsgContentInfo);
        *pMsgContentInfo = NULL;
    }
}

/**
 * Common Process UI destroy event
 * param (in) VOID
 * return  void
 */
VOID Sms_Common_Free_ContactInfo(VOID)
{
    Msg_Contact_List_Info_t *pNode = NULL;

    if (NULL != g_MsgGlobalCtrl.pContactInfo)
    {
        pNode = _lv_ll_get_head(g_MsgGlobalCtrl.pContactInfo);
        while (NULL != pNode)
        {
            Hal_Mem_Free(pNode->pContact);
            pNode->pContact = NULL;
            Hal_Mem_Free(pNode->pNum);
            pNode->pNum = NULL;
            pNode       = _lv_ll_get_next(g_MsgGlobalCtrl.pContactInfo, pNode);
        }
        _lv_ll_clear(g_MsgGlobalCtrl.pContactInfo);
        Hal_Mem_Free(g_MsgGlobalCtrl.pContactInfo);
        g_MsgGlobalCtrl.pContactInfo = NULL;
    }
    g_MsgGlobalCtrl.ContactCnt = 0;
    g_MsgGlobalCtrl.SendCnt    = 0;
    g_MsgGlobalCtrl.ResendCnt  = 0;
    g_MsgGlobalCtrl.SuccessCnt = 0;
}

/**
 * Common Process UI destroy event
 * param (in) pDesc:  VOID *
 * return  void
 */
VOID Sms_Common_All_Global_Destory(VOID *pDesc)
{
    Hal_Mem_Free(g_MsgGlobalCtrl.pDistriListName);
    g_MsgGlobalCtrl.pDistriListName = NULL;
    Hal_Mem_Free(g_MsgGlobalCtrl.pContact);
    g_MsgGlobalCtrl.pContact = NULL;
    Hal_Mem_Free(g_MsgGlobalCtrl.pContactTa);
    g_MsgGlobalCtrl.pContactTa = NULL;
    Sms_Common_Free_Msg_Content_Info(&g_MsgGlobalCtrl.pInboxMsgContentInfo);
    Sms_Common_Free_Msg_Content_Info(&g_MsgGlobalCtrl.pSentboxMsgContentInfo);
    Sms_Common_Free_Msg_Content_Info(&g_MsgGlobalCtrl.pDraftMsgContentInfo);
    Sms_Common_Free_Msg_Content_Info(&g_MsgGlobalCtrl.pScrnMsgContentInfo);
    Sms_Common_Free_ContactInfo();
    if (NULL != g_MsgGlobalCtrl.pDisList)
    {
        if (g_MsgGlobalCtrl.pDisList->DistriCnt)
        {
            _lv_ll_clear(g_MsgGlobalCtrl.pDisList->pDisListInfo);
            Hal_Mem_Free(g_MsgGlobalCtrl.pDisList->pDisListInfo);
            g_MsgGlobalCtrl.pDisList->pDisListInfo = NULL;
        }
        Hal_Mem_Free(g_MsgGlobalCtrl.pDisList);
        g_MsgGlobalCtrl.pDisList = NULL;
    }
    g_MsgGlobalCtrl.pCurrentViewMsg = NULL;
    g_MsgGlobalCtrl.pCurrentSend    = NULL;
    g_MsgGlobalCtrl.pChart          = NULL;
    Hal_Mem_Free(g_MsgGlobalCtrl.pTime);
    g_MsgGlobalCtrl.pTime = NULL;
    Hal_Mem_Free(g_MsgGlobalCtrl.pSmsText);
    g_MsgGlobalCtrl.pSmsText = NULL;
    Hal_Mem_Free(g_MsgGlobalCtrl.pReadSmsText);
    g_MsgGlobalCtrl.pReadSmsText = NULL;

    g_MsgGlobalCtrl.CurOpenBox   = INVAILD_ID;
    g_MsgGlobalCtrl.ResendFlg    = false;
    g_MsgGlobalCtrl.InsertNumFlg = false;
    g_MsgGlobalCtrl.OtherAppUseType = 0;
    Hal_Mem_Free(pDesc);
    pDesc = NULL;
} /* Sms_Common_All_Global_Destory */

/**
 * Common function for delete sms
 * param (in) BOOL DeleteType,VOID *pInput
 * return  void
 */
VOID Sms_Common_Delete_Sms(BOOL DeleteType, VOID **pInput)
{
    UINT32                        i                = 0;
    UINT32                        j                = 0;
    MMI_Modem_Delete_Sms_Req_t    *pDeleteSmsSim1  = NULL;
    MMI_Modem_Delete_Sms_Req_t    *pDeleteSmsSim2  = NULL;
    Msg_Content_Info_t            *pTemp           = NULL;
    Msg_Content_Info_t            *pTemp1          = NULL;
    MMI_Modem_Read_Sms_Info_Cnf_t *pReadSmsInfoCnf = NULL;
    lv_ll_t                       *pbox            = NULL;
    BOOL                          SmsType;

    switch (DeleteType)
    {
    case MSG_DEL_INBOX_ONEBYONE:
    case DEL_SCREENED_MESSAGES_ONEBYONE:
        SmsType = MMI_MODEM_UNSENT;
        break;

    case MSG_DEL_SENT_ITEMS_ONEBYONE:
        SmsType = MMI_MODEM_READ;
        break;

    case MSG_DEL_INBOX_ALLREAD:
    case DEL_SCREENED_MESSAGES_ALLREAD:
        SmsType = MMI_MODEM_READ;
        break;
    }

    if (MSG_DEL_ALL_READ == DeleteType)
    {
        pReadSmsInfoCnf = (MMI_Modem_Read_Sms_Info_Cnf_t *)(*pInput);

        pDeleteSmsSim1 = (MMI_Modem_Delete_Sms_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modem_Delete_Sms_Req_t));

        Hal_Mem_Set(pDeleteSmsSim1, 0, sizeof(MMI_Modem_Delete_Sms_Req_t));

        pDeleteSmsSim2 = (MMI_Modem_Delete_Sms_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modem_Delete_Sms_Req_t));

        Hal_Mem_Set(pDeleteSmsSim2, 0, sizeof(MMI_Modem_Delete_Sms_Req_t));
        // send all marked sms to adapter
        for (i = 0; i < pReadSmsInfoCnf->ReadMsgCnt; i++)
        {
            if (MMI_MODEM_UNREAD != pReadSmsInfoCnf->pMsgContentInfo[i].ReadSmsType)
            {
                if (MMI_MODEM_SIM_1 == pReadSmsInfoCnf->pMsgContentInfo[i].SimId)
                {
                    pDeleteSmsSim1->DeleteMsgCnt++;
                }
                else
                {
                    pDeleteSmsSim2->DeleteMsgCnt++;
                }
            }
        }
        if (pDeleteSmsSim1->DeleteMsgCnt)
        {
            pDeleteSmsSim1->pMsgIndex = (UINT32 *)Hal_Mem_Alloc(sizeof(UINT32) * pDeleteSmsSim1->DeleteMsgCnt);
            Hal_Mem_Set(pDeleteSmsSim1->pMsgIndex, 0, sizeof(UINT32) * pDeleteSmsSim1->DeleteMsgCnt);
            pDeleteSmsSim1->SimId = MMI_MODEM_SIM_1;
            for (i = 0; i < pReadSmsInfoCnf->ReadMsgCnt; i++)
            {
                if (MMI_MODEM_UNREAD != pReadSmsInfoCnf->pMsgContentInfo[i].ReadSmsType)
                {
                    if (MMI_MODEM_SIM_1 == pReadSmsInfoCnf->pMsgContentInfo[i].SimId)
                    {
                        Hal_Mem_Copy(&pDeleteSmsSim1->pMsgIndex[j], &pReadSmsInfoCnf->pMsgContentInfo[i].MsgIndex, sizeof(UINT32));
                        j++;
                    }
                }
            }
        }
        else
        {
            Hal_Mem_Free(pDeleteSmsSim1);
            pDeleteSmsSim1 = NULL;
        }
        if (pDeleteSmsSim2->DeleteMsgCnt)
        {
            pDeleteSmsSim2->pMsgIndex = (UINT32 *)Hal_Mem_Alloc(sizeof(UINT32) * pDeleteSmsSim2->DeleteMsgCnt);
            Hal_Mem_Set(pDeleteSmsSim2->pMsgIndex, 0, sizeof(UINT32) * pDeleteSmsSim2->DeleteMsgCnt);
            pDeleteSmsSim2->SimId = MMI_MODEM_SIM_2;
            j                     = 0;
            for (i = 0; i < pReadSmsInfoCnf->ReadMsgCnt; i++)
            {
                if (MMI_MODEM_UNREAD != pReadSmsInfoCnf->pMsgContentInfo[i].ReadSmsType)
                {
                    if (MMI_MODEM_SIM_2 == pReadSmsInfoCnf->pMsgContentInfo[i].SimId)
                    {
                        Hal_Mem_Copy(&pDeleteSmsSim2->pMsgIndex[j], &pReadSmsInfoCnf->pMsgContentInfo[i].MsgIndex, sizeof(UINT32));
                        j++;
                    }
                }
            }
        }
        else
        {
            Hal_Mem_Free(pDeleteSmsSim2);
            pDeleteSmsSim2 = NULL;
        }
    }
    else if (  (MSG_DEL_INBOX_ONEBYONE == DeleteType)
            || (MSG_DEL_SENT_ITEMS_ONEBYONE == DeleteType)
            || (DEL_SCREENED_MESSAGES_ONEBYONE == DeleteType))
    {
        pbox           = (lv_ll_t *)(*pInput);
        pDeleteSmsSim1 = (MMI_Modem_Delete_Sms_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modem_Delete_Sms_Req_t));

        Hal_Mem_Set(pDeleteSmsSim1, 0, sizeof(MMI_Modem_Delete_Sms_Req_t));

        pDeleteSmsSim2 = (MMI_Modem_Delete_Sms_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modem_Delete_Sms_Req_t));

        Hal_Mem_Set(pDeleteSmsSim2, 0, sizeof(MMI_Modem_Delete_Sms_Req_t));
        // send all marked sms to adapter
        pTemp = _lv_ll_get_head(pbox);

        while (NULL != pTemp)
        {
            if (SmsType == pTemp->SmsType)
            {
                if (MMI_MODEM_SIM_1 == pTemp->SimId)
                {
                    pDeleteSmsSim1->DeleteMsgCnt++;
                }
                else
                {
                    pDeleteSmsSim2->DeleteMsgCnt++;
                }
            }
            pTemp = _lv_ll_get_next(pbox, pTemp);
        }
        if (pDeleteSmsSim1->DeleteMsgCnt)
        {
            pDeleteSmsSim1->pMsgIndex = (UINT32 *)Hal_Mem_Alloc(sizeof(UINT32) * pDeleteSmsSim1->DeleteMsgCnt);
            Hal_Mem_Set(pDeleteSmsSim1->pMsgIndex, 0, sizeof(UINT32) * pDeleteSmsSim1->DeleteMsgCnt);
            pDeleteSmsSim1->SimId = MMI_MODEM_SIM_1;
            pTemp                 = _lv_ll_get_head(pbox);

            while (NULL != pTemp)
            {
                if (SmsType == pTemp->SmsType)
                {
                    if (MMI_MODEM_SIM_1 == pTemp->SimId)
                    {
                        Hal_Mem_Copy(&pDeleteSmsSim1->pMsgIndex[i], &pTemp->MsgIndex, sizeof(UINT32));
                        pTemp1 = pTemp;
                        i++;
                    }
                }
                pTemp = _lv_ll_get_next(pbox, pTemp);
                if (NULL != pTemp1)
                {
                    _lv_ll_remove(pbox, pTemp1);
                    Hal_Mem_Free(pTemp1);
                    pTemp1 = NULL;
                }
            }
        }
        else
        {
            Hal_Mem_Free(pDeleteSmsSim1);
            pDeleteSmsSim1 = NULL;
        }
        if (pDeleteSmsSim2->DeleteMsgCnt)
        {
            pDeleteSmsSim2->pMsgIndex = (UINT32 *)Hal_Mem_Alloc(sizeof(UINT32) * pDeleteSmsSim2->DeleteMsgCnt);
            Hal_Mem_Set(pDeleteSmsSim2->pMsgIndex, 0, sizeof(UINT32) * pDeleteSmsSim2->DeleteMsgCnt);
            pDeleteSmsSim2->SimId = MMI_MODEM_SIM_2;
            pTemp                 = _lv_ll_get_head(pbox);
            i                     = 0;
            while (NULL != pTemp)
            {
                if (SmsType == pTemp->SmsType)
                {
                    if (MMI_MODEM_SIM_2 == pTemp->SimId)
                    {
                        Hal_Mem_Copy(&pDeleteSmsSim2->pMsgIndex[i], &pTemp->MsgIndex, sizeof(UINT32));
                        pTemp1 = pTemp;
                        i++;
                    }
                }
                pTemp = _lv_ll_get_next(pbox, pTemp);
                if (NULL != pTemp1)
                {
                    _lv_ll_remove(pbox, pTemp1);
                    Hal_Mem_Free(pTemp1);
                    pTemp1 = NULL;
                }
            }
        }
        else
        {
            Hal_Mem_Free(pDeleteSmsSim2);
            pDeleteSmsSim2 = NULL;
        }
        if (NULL == pbox->head)
        {
            Hal_Mem_Free(pbox);
            (*pInput) = NULL;
        }
    }
    else if (  (MSG_DEL_INBOX_ALLREAD == DeleteType)
            || (DEL_SCREENED_MESSAGES_ALLREAD == DeleteType))
    {
        pbox           = (lv_ll_t *)(*pInput);
        pDeleteSmsSim1 = (MMI_Modem_Delete_Sms_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modem_Delete_Sms_Req_t));

        Hal_Mem_Set(pDeleteSmsSim1, 0, sizeof(MMI_Modem_Delete_Sms_Req_t));

        pDeleteSmsSim2 = (MMI_Modem_Delete_Sms_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modem_Delete_Sms_Req_t));

        Hal_Mem_Set(pDeleteSmsSim2, 0, sizeof(MMI_Modem_Delete_Sms_Req_t));
        // send all marked sms to adapter
        pTemp = _lv_ll_get_head(pbox);

        while (NULL != pTemp)
        {
            if (SmsType == pTemp->SmsType)
            {
                if (MMI_MODEM_SIM_1 == pTemp->SimId)
                {
                    pDeleteSmsSim1->DeleteMsgCnt++;
                }
                else
                {
                    pDeleteSmsSim2->DeleteMsgCnt++;
                }
            }
            pTemp = _lv_ll_get_next(pbox, pTemp);
        }
        if (pDeleteSmsSim1->DeleteMsgCnt)
        {
            pDeleteSmsSim1->pMsgIndex = (UINT32 *)Hal_Mem_Alloc(sizeof(UINT32) * pDeleteSmsSim1->DeleteMsgCnt);
            Hal_Mem_Set(pDeleteSmsSim1->pMsgIndex, 0, sizeof(UINT32) * pDeleteSmsSim1->DeleteMsgCnt);
            pDeleteSmsSim1->SimId = MMI_MODEM_SIM_1;
            pTemp                 = _lv_ll_get_head(pbox);

            while (NULL != pTemp)
            {
                if (SmsType == pTemp->SmsType)
                {
                    if (MMI_MODEM_SIM_1 == pTemp->SimId)
                    {
                        Hal_Mem_Copy(&pDeleteSmsSim1->pMsgIndex[i], &pTemp->MsgIndex, sizeof(UINT32));
                        pTemp1 = pTemp;
                        i++;
                    }
                }
                pTemp = _lv_ll_get_next(pbox, pTemp);
                if (NULL != pTemp1)
                {
                    _lv_ll_remove(pbox, pTemp1);
                    Hal_Mem_Free(pTemp1);
                    pTemp1 = NULL;
                }
            }
        }
        else
        {
            Hal_Mem_Free(pDeleteSmsSim1);
            pDeleteSmsSim1 = NULL;
        }
        if (pDeleteSmsSim2->DeleteMsgCnt)
        {
            pDeleteSmsSim2->pMsgIndex = (UINT32 *)Hal_Mem_Alloc(sizeof(UINT32) * pDeleteSmsSim2->DeleteMsgCnt);
            Hal_Mem_Set(pDeleteSmsSim2->pMsgIndex, 0, sizeof(UINT32) * pDeleteSmsSim2->DeleteMsgCnt);
            pDeleteSmsSim2->SimId = MMI_MODEM_SIM_2;
            pTemp                 = _lv_ll_get_head(pbox);
            i                     = 0;
            while (NULL != pTemp)
            {
                if (SmsType == pTemp->SmsType)
                {
                    if (MMI_MODEM_SIM_2 == pTemp->SimId)
                    {
                        Hal_Mem_Copy(&pDeleteSmsSim2->pMsgIndex[i], &pTemp->MsgIndex, sizeof(UINT32));
                        pTemp1 = pTemp;
                        i++;
                    }
                }
                pTemp = _lv_ll_get_next(pbox, pTemp);
                if (NULL != pTemp1)
                {
                    _lv_ll_remove(pbox, pTemp1);
                    Hal_Mem_Free(pTemp1);
                    pTemp1 = NULL;
                }
            }
        }
        else
        {
            Hal_Mem_Free(pDeleteSmsSim2);
            pDeleteSmsSim2 = NULL;
        }
        if (NULL == pbox->head)
        {
            Hal_Mem_Free(pbox);
            (*pInput) = NULL;
        }
    }
    else if (  (MSG_DEL_INBOX_ALL == DeleteType)
            || (MSG_DEL_SENT_ITEMS_ALLREAD == DeleteType)
            || (DEL_SCREENED_MESSAGES_ALL == DeleteType))
    {
        pbox           = (lv_ll_t *)pInput;
        pDeleteSmsSim1 = (MMI_Modem_Delete_Sms_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modem_Delete_Sms_Req_t));

        Hal_Mem_Set(pDeleteSmsSim1, 0, sizeof(MMI_Modem_Delete_Sms_Req_t));

        pDeleteSmsSim2 = (MMI_Modem_Delete_Sms_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modem_Delete_Sms_Req_t));

        Hal_Mem_Set(pDeleteSmsSim2, 0, sizeof(MMI_Modem_Delete_Sms_Req_t));
        // send all marked sms to adapter
        pTemp = _lv_ll_get_head(pbox);

        while (NULL != pTemp)
        {
            if (MMI_MODEM_SIM_1 == pTemp->SimId)
            {
                pDeleteSmsSim1->DeleteMsgCnt++;
            }
            else
            {
                pDeleteSmsSim2->DeleteMsgCnt++;
            }

            pTemp = _lv_ll_get_next(pbox, pTemp);
        }
        if (pDeleteSmsSim1->DeleteMsgCnt)
        {
            pDeleteSmsSim1->pMsgIndex = (UINT32 *)Hal_Mem_Alloc(sizeof(UINT32) * pDeleteSmsSim1->DeleteMsgCnt);
            Hal_Mem_Set(pDeleteSmsSim1->pMsgIndex, 0, sizeof(UINT32) * pDeleteSmsSim1->DeleteMsgCnt);
            pDeleteSmsSim1->SimId = MMI_MODEM_SIM_1;
            pTemp                 = _lv_ll_get_head(pbox);

            while (NULL != pTemp)
            {
                if (MMI_MODEM_SIM_1 == pTemp->SimId)
                {
                    Hal_Mem_Copy(&pDeleteSmsSim1->pMsgIndex[i], &pTemp->MsgIndex, sizeof(UINT32));
                    pTemp1 = pTemp;
                    i++;
                }
                pTemp = _lv_ll_get_next(pbox, pTemp);
                if (NULL != pTemp1)
                {
                    _lv_ll_remove(pbox, pTemp1);
                    Hal_Mem_Free(pTemp1);
                    pTemp1 = NULL;
                }
            }
        }
        else
        {
            Hal_Mem_Free(pDeleteSmsSim1);
            pDeleteSmsSim1 = NULL;
        }
        if (pDeleteSmsSim2->DeleteMsgCnt)
        {
            pDeleteSmsSim2->pMsgIndex = (UINT32 *)Hal_Mem_Alloc(sizeof(UINT32) * pDeleteSmsSim2->DeleteMsgCnt);
            Hal_Mem_Set(pDeleteSmsSim2->pMsgIndex, 0, sizeof(UINT32) * pDeleteSmsSim2->DeleteMsgCnt);
            pDeleteSmsSim2->SimId = MMI_MODEM_SIM_2;
            pTemp                 = _lv_ll_get_head(pbox);
            i                     = 0;
            while (NULL != pTemp)
            {
                if (MMI_MODEM_SIM_2 == pTemp->SimId)
                {
                    Hal_Mem_Copy(&pDeleteSmsSim2->pMsgIndex[i], &pTemp->MsgIndex, sizeof(UINT32));
                    pTemp1 = pTemp;
                    i++;
                }
                pTemp = _lv_ll_get_next(pbox, pTemp);
                if (NULL != pTemp1)
                {
                    _lv_ll_remove(pbox, pTemp1);
                    Hal_Mem_Free(pTemp1);
                    pTemp1 = NULL;
                }
            }
        }
        else
        {
            Hal_Mem_Free(pDeleteSmsSim2);
            pDeleteSmsSim2 = NULL;
        }
        if (NULL == pbox->head)
        {
            Hal_Mem_Free(pbox);
            (*pInput) = NULL;
        }
    }
    if (NULL != pDeleteSmsSim1)
    {
        MMI_Modem_Delete_Sms_Req(pDeleteSmsSim1);
    }
    if (NULL != pDeleteSmsSim2)
    {
        MMI_Modem_Delete_Sms_Req(pDeleteSmsSim2);
    }
} /* Sms_Common_Delete_Sms */

/**
 * Common function for delete sms
 * param (in) lv_ll_t **pInput
 * return  void
 */
VOID Sms_Common_Delete_Single_Sms(lv_ll_t **pInput)
{
    MMI_Modem_Delete_Sms_Req_t *pDeleteSms = NULL;
    lv_ll_t                    *pbox       = *pInput;

    pDeleteSms = (MMI_Modem_Delete_Sms_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modem_Delete_Sms_Req_t));

    Hal_Mem_Set(pDeleteSms, 0, sizeof(MMI_Modem_Delete_Sms_Req_t));

    pDeleteSms->DeleteMsgCnt = 1;
    pDeleteSms->pMsgIndex    = (UINT32 *)Hal_Mem_Alloc(sizeof(UINT32));
    *(pDeleteSms->pMsgIndex) = g_MsgGlobalCtrl.pCurrentViewMsg->MsgIndex;
    pDeleteSms->SimId        = g_MsgGlobalCtrl.pCurrentViewMsg->SimId;
    _lv_ll_remove(pbox, g_MsgGlobalCtrl.pCurrentViewMsg);
    Hal_Mem_Free(g_MsgGlobalCtrl.pCurrentViewMsg->pContact);
    g_MsgGlobalCtrl.pCurrentViewMsg->pContact = NULL;
    Hal_Mem_Free(g_MsgGlobalCtrl.pCurrentViewMsg);
    g_MsgGlobalCtrl.pCurrentViewMsg = NULL;
    if (NULL == _lv_ll_get_head(pbox))
    {
        Hal_Mem_Free(pbox);
        (*pInput) = NULL;
    }
    MMI_Modem_Delete_Sms_Req(pDeleteSms);
}

/**
 * check status of SIM
 * param(in) void:
 * return UINT8 SMS_SIM_STATUS
 */
SMS_SIM_STATUS Sms_Get_Sim_Status(VOID)
{
    SMS_SIM_STATUS SimStatus = SMS_NO_SIM_PRESENT;

    if (MMI_Modem_Sim_Present_Check_Req(MMI_MODEM_SIM_1))
    {
        SimStatus |= SMS_SIM1_PRESENT;
    }

    if (MMI_Modem_Sim_Present_Check_Req(MMI_MODEM_SIM_2))
    {
        SimStatus |= SMS_SIM2_PRESENT;
    }

    return(SimStatus);
}
